<script type="text/javascript">
    $(function() {
        $("#desde").datepicker({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true});
        $( "#hasta" ).datepicker({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true});
    });
</script> 


<div class="row-fluid">
    <?php echo $this->render('/listados/menu.phtml'); ?>


    <div class="well well-small">
        <legend>
            Deuda de Creditos
            <button type="button" id="btnPrint" class="btn btn-inverse noPrint" style="float: right;"><i class=" icon-print icon-white"></i>  Imprimir</button>
        </legend>
        <form name="form_busqueda" class="form-inline noPrint" method="GET" action="<?php echo $this->baseUrl(); ?>/admin/listados/deudas">
            <label class="control-label">Desde</label>
            <input id="desde" name="desde" value="<?php if (empty($this->desde)) { echo '1'; } else { echo $this->desde; } ?>" class="input-small" type="text" />
            <label class="control-label">Hasta</label>
            <input id="hasta" name="hasta"   value="<?php if (empty($this->hasta)) { echo '1'; } else { echo $this->hasta; } ?>" class="input-small" type="text" />
            
            <label class="control-label">Cant. dias de Mora</label>
            <input id="dias_mora" name="dias_mora"   value="<?php if (empty($this->dias_mora)) { echo '0'; } else { echo $this->dias_mora; } ?>" class="input-small" type="text" />
            
            <label>Sucursal</label>
            <select id="sucursal" name="sucursal">
                <option value="0">Todas</option>
                <?php foreach ($this->sucursales as $sucursal) { ?>
                <option <?php echo $this->sucursal == $sucursal->getId() ? "selected" : "" ?> value="<?php echo $sucursal->getId(); ?>">
                    <?php echo $sucursal->getDescripcion(); ?></option>
                <?php } ?>
            </select>
            
            <input class="btn" value="Buscar" type="submit" />
        </form>

        <div class="row-fluid ">
            
            <table class="table table-condensed tabla_reducida" style="font-size: 12px;">
                <thead>
                    <tr>
                        <th>Cant. Total</th><th>Cant. Mososos</th><th>Total Financiado</th><th>Total Financiado Morosos</th><th>Deuda</th>
                    </tr>
                </thead>
                <tbody style="font-size: 24px;font-weight: bold;">
                    <tr>
                        <td><?php echo $this->cantidadDeCreditos; ?></td>
                        <td><?php echo $this->cantidadDeCreditosMorosos; ?></td>
                        <td><?php echo number_format($this->monto_total_financiado, 2, ",", ".") . " $" ?></td>
                        <td><?php echo number_format($this->monto_total_financiado_por_morosos, 2, ",", ".") . " $" ?></td>
                        <td><?php echo $this->deuda . " %"; ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="row-fluid">
    <div class="well well-small">
        <legend>Mes A Mes Por Sucursal
            
                <button id="exportar_excel" class="btn btn-success noPrint pull-right" type="button"><i class=" icon-download icon-white"></i>  Exportar a Excel</button>
            
        </legend>

        <table class="tabla_reducida" style="font-size: 12px;">
            <thead>
                <tr>
                    <th style="width: 150px;">Mes</th><th></th>
                </tr>
            </thead>
            <tbody style="font-size: 19px;font-weight: bold;">
                <?php foreach ($this->creditos_por_sucursal_por_mes as $mes => $datos): ?>
                <tr>
                    <?php $mes = explode("/", $mes) ?>
                    <td><?php echo $this->common->descripcionMes(intval($mes[0])) . " de ". $mes[1]; ?></td>
                    <td style="padding:0;">
                        <table style="width:100%;padding:0;margin:0;">
                            <thead style="font-size: 10px;font-weight:bold;">
                            <tr>
                                <th>Sucur.</th><th>Cant. Total</th><th>Cant. Mososos</th><th>Total Financiado</th><th>Total Financiado Morosos</th><th>Deuda</th>
                            </tr>
                            </thead>
                    <?php foreach ($datos as $suc => $datos_suc): ?>
                    
                        <tr>
                            <td><?php echo $suc ?></td>
                            <td><?php echo $datos_suc["cant"] ?></td>
                            <td><?php echo $datos_suc["cant_morosos"] ?></td>
                            <td><?php echo number_format($datos_suc["monto"], 2, ",", ".") . " $" ?></td>
                            <td><?php echo number_format($datos_suc["monto_morosos"], 2, ",", ".") . " $" ?></td>
                            <td>
                                <?php if ($datos_suc["monto"] > 0): ?>
                                <?php echo round((($datos_suc["monto_morosos"] * 100) / $datos_suc["monto"]), 2) . " %"; ?>
                                <?php else: ?>
                                <?php echo "0 %" ?>
                                <?php endif; ?>
                            </td>
                        </tr>
                    
                    <?php endforeach; ?>
                        </table>
                    <td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
    </div>
</div>

<?php if (empty($this->sucursal) || $this->sucursal == 0): ?>
<div class="row-fluid">
    <div class="well well-small">
        <legend>Mes A Mes General</legend>
        
        <table id="mes_a_mes_general" class="tabla_reducida" style="font-size: 12px;">
            <thead>
                <tr>
                    <th>Mes</th><th>Cant. Total</th><th>Cant. Mososos</th><th>Total Financiado</th><th>Total Financiado Morosos</th><th>Deuda</th>
                </tr>
            </thead>
            <tbody style="font-size: 24px;font-weight: bold;">
                <?php foreach ($this->creditos_por_mes as $mes => $datos): ?>
                <tr>
                    <?php $mes = explode("/", $mes) ?>
                    <td><?php echo $this->common->descripcionMes(intval($mes[0])) ." de ". $mes[1]; ?></td>
                    <td><?php echo $datos["cant"] ?></td>
                    <td><?php echo $datos["cant_morosos"] ?></td>
                    <td><?php echo number_format($datos["monto"], 2, ",", ".") . " $" ?></td>
                    <td><?php echo number_format($datos["monto_morosos"], 2, ",", ".") . " $" ?></td>
                    <td>
                        <?php if ($datos["monto"] > 0): ?>
                        <?php echo round((($datos["monto_morosos"] * 100) / $datos["monto"]), 2) . " %"; ?>
                        <?php else: ?>
                        <?php echo "0 %" ?>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
    </div>
</div>
<?php endif; ?>

<div class="row-fluid">
    <div class="well well-small">
        <legend>
            Creditos Morosos
            <button type="button" onclick="window.print()" class="btn btn-inverse noPrint" style="float: right;"><i class=" icon-print icon-white"></i>  Imprimir</button>&nbsp;&nbsp;
        </legend>
        <table id="lista_morosos" class="tabla_reducida">
            <thead>
            <th>Otorgado</th><th>Nro Cred.</th><th>DNI</th><th>Monto</th><th>Monto Financ.</th><th>Plan</th><th>Tipo</th><th>Estado</th></th>
            </thead>
            <tbody>
                <?php if (count($this->creditos)): ?>
                    <?php $total = 0;
                    $totalFinanciado = 0; ?>
                    <?php $credito = new Doctrine_Model_Entities_Credito() ?>
                    <?php foreach ($this->creditos as $credito): ?>
                        <?php
                        $total += $credito->getMonto();
                        $totalFinanciado += $credito->obtenerMontoFinanciado();
                        ?>
                        <tr>
                            <td><?php echo $credito->getFecha()->format('d/m/Y'); ?></td>
                            <td><a href="<?php echo $this->baseUrl("usuarios/busqueda/obternerdatospersona/persona_id/{$credito->getCliente()->getId()}") ?>"><?php echo $credito->getNumero(); ?></a></td>
                            <td style="width:90px;text-align:center;"><?php echo $credito->getCliente()->getNroDocumento(); ?></td>
                            <td><?php echo $credito->getMonto(); ?></td>
                            <td><?php echo $credito->obtenerMontoFinanciado(); ?></td>
                            <td><?php echo $credito->getTasa()->getFormula()->getDescripcion() ?></td>
                            <td><?php echo $credito->getTipo()->getDescripcion() ?></td>

                            <td><?php echo $credito->getEstado()->getDescripcion(); ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <tr>
                        <td colspan="3"></td>
                        <td><?php echo $total; ?></td>
                        <td><?php echo $totalFinanciado; ?></td>
                    </tr>
                <?php else: ?>
                    <tr>
                        <td colspan="9">Sin Resultados</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">
    
    $(function() {
    
        $("#btnPrint").live("click", function () {
            window.print();
        });
    
    });
</script>

<script type="text/javascript">
    $(document).ready(function() {
        
        $("#exportar_excel").click(function(){
            var desde = $("#desde").val().replace("/","-").replace("/","-");
            var hasta = $("#hasta").val().replace("/","-").replace("/","-");
            var dias_mora = $("#dias_mora").val();
            var sucursal = $("#sucursal option:selected").val();
            
            var url = "<?php echo $this->baseUrl("/admin/listados/exportardeuda"); ?>";
            url += "/desde/"+desde+"/hasta/"+hasta+"/dias_mora/"+dias_mora+"/sucursal/"+sucursal;
            $(location).attr("href", url);           
        });
        
    });
</script>